<!--
 * @Author: boyyang
 * @Date: 2022-01-16 22:06:22
 * @LastEditTime: 2022-03-18 22:01:36
 * @LastEditors: boyyang
 * @Description: 
 * @FilePath: \boyyang\src\components\imagesUpload\index.vue
-->

<template>
    <div class="image-upload" v-if="_isShow" @click="closeUpload">
        <div :class="['image-upload-box', { 'image-upload-box-leave': !props.isShow }]" @click.stop>
            <div class="img-preview">
                <template v-if="!upFileType">
                    <img :src="props.url" v-if="props.url" />
                    <i class="iconfont icon-tupian up-bg" v-else></i>
                </template>
                <template v-else>
                    <video :src="props.url" v-if="props.url" autoplay loop muted />
                    <i class="iconfont icon-bofang4 up-bg" v-else></i>
                </template>
            </div>
            <div class="bottom-btn-group">
                <template v-if="!upFileType">
                    <div class="bottom-btn">
                        <custom-upload
                            :file-type="acceptType"
                            :multiple="true"
                            @on-change="e => uploadClick(e, 0)"
                        >
                            <tool-tip text="上传电脑壁纸">
                                <template #icon>
                                    <i class="iconfont icon-bg-pc"></i>
                                </template>
                            </tool-tip>
                        </custom-upload>

                        <custom-upload
                            :file-type="acceptType"
                            :multiple="true"
                            @on-change="e => uploadClick(e, 1)"
                        >
                            <tool-tip text="上传手机壁纸">
                                <template #icon>
                                    <i class="iconfont icon-shoujitianchong"></i>
                                </template>
                            </tool-tip>
                        </custom-upload>
                    </div>
                </template>
                <template v-else>
                    <div class="bottom-btn">
                        <custom-upload
                            :file-type="acceptType"
                            :file-size="200"
                            @on-change="e => uploadClick(e, 2)"
                        >
                            <tool-tip text="上传视频">
                                <template #icon>
                                    <i class="iconfont icon-yinle6"></i>
                                </template>
                            </tool-tip>
                        </custom-upload>
                    </div>
                </template>
            </div>

            <div class="btn-close" @click="closeUpload">
                <i class="iconfont icon-jiahao"></i>
            </div>

            <div class="btn-change" @click="changeUpType">
                <i class="iconfont icon-qiehuangongsi"></i>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { IUploadRes } from '@/components/customUpload/index.vue'
interface IImagesUploadProps {
    isShow?: boolean;
    url?: string;
}

interface IImagesUploadEmits {
    (e: 'fileChange', options: IUploadRes, type: number): void;
    (e: 'update:isShow', options: boolean): void;
}

const props = withDefaults(defineProps<IImagesUploadProps>(), {
    isShow: false
})

const emits = defineEmits<IImagesUploadEmits>()

const _isShow = ref<boolean>(false)
const upType = ref<number>(0)
const upFileType = ref<boolean>(false)
const acceptType = ref<string>('image/*')

watch(() => props.isShow, (n_val, o_val) => {
    if (n_val) {
        _isShow.value = true
    } else {
        setTimeout(() => {
            _isShow.value = false
        }, 500);
    }
})

const uploadClick = (e: IUploadRes, type: number) => {
    upType.value = type
    emits('fileChange', e, upType.value)
}

const closeUpload = () => {
    emits('update:isShow', false)
}

const changeUpType = () => {
    upFileType.value = !upFileType.value

    if (upFileType.value) {
        acceptType.value = 'video/mp4'
    } else {
        acceptType.value = 'image/*'
    }
}

</script>

<style scoped lang="scss">
.image-upload {
    box-sizing: border-box;
    width: 100vw;
    height: 100vh;

    background-color: rgba(0, 0, 0, 0.3);
    z-index: 99;

    position: absolute;
    top: 0;
    left: 0;

    display: flex;
    justify-content: center;
    align-items: center;

    overflow: hidden;

    .image-upload-box {
        box-sizing: border-box;
        width: 500px;
        height: 350px;
        background-color: #3d3b4f;
        border-radius: 10px;

        display: flex;
        justify-content: center;
        align-items: center;

        position: relative;

        animation: move 1s cubic-bezier(0.19, 1, 0.22, 1);

        @keyframes move {
            0% {
                transform: translateX(-200%);
            }
        }
    }

    .image-upload-box-leave {
        animation: move1 0.5s cubic-bezier(0.19, 1, 0.22, 1);

        @keyframes move1 {
            100% {
                transform: translateX(250%);
            }
        }
    }

    .bottom-btn-group {
        position: absolute;
        bottom: 0;
        display: flex;
    }

    .bottom-btn {
        width: 100px;
        height: 35px;
        background-color: #44cef6;

        display: flex;
        justify-content: space-around;
        align-items: center;

        border-radius: 7px 7px 0 0;

        i {
            font-size: 20px;
            font-weight: 500;
            transition: all 0.3s linear;
            cursor: pointer;
            &:hover {
                color: whitesmoke;
            }
        }
    }

    .btn-close {
        width: 30px;
        height: 30px;
        position: absolute;
        top: 0;
        right: 0;
        background-color: #44cef6;

        display: flex;
        justify-content: center;
        align-items: center;

        border-radius: 0 10px 0 0;

        cursor: pointer;

        i {
            font-size: 25px;
            transition: all 0.5s linear;
            transform: rotateZ(45deg);
        }

        &:hover {
            i {
                color: whitesmoke;
                transform: rotateZ(135deg);
            }
        }
    }

    .btn-change {
        width: 30px;
        height: 30px;
        position: absolute;
        top: 0;
        left: 0;
        background-color: #44cef6;

        display: flex;
        justify-content: center;
        align-items: center;

        border-radius: 10px 0 0;

        cursor: pointer;

        i {
            font-size: 20px;
            transition: all 0.5s linear;
            transform: rotateZ(45deg);

            animation: trans 2s linear infinite;

            &:hover {
                color: whitesmoke;
            }
        }

        @keyframes trans {
            0% {
                transform: rotateZ(0deg);
            }

            100% {
                transform: rotateZ(360deg);
            }
        }
    }

    // .upload {
    //     display: none;
    // }

    .img-preview {
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        overflow: hidden;
        border-radius: 5px;
        img {
            height: 100%;
        }

        video {
            height: 100%;
        }
    }

    .up-bg {
        font-size: 40px;
        color: whitesmoke;
    }
}

@media screen and (max-width: 500px) {
    .image-upload {
        .image-upload-box {
            width: 350px;
        }
    }
}
</style>
